//
//  PhotoBrowserViewController.swift
//  PhotoBrowser
//
//  Created by 游强辉 on 16/4/28.
//  Copyright © 2016年 youqianghui. All rights reserved.
//

import UIKit

private let photoCellID = "photoCell"

class PhotoBrowserViewController: UIViewController {

    //MARK:- 成员属性
    var pictureArray : [Picture]?
    var pictureIndexPath : NSIndexPath?
    
    //MARK:- 控件
    private lazy var collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoFlowLayout())
    
    private lazy var closeButton = UIButton(title: "关 闭", bgColor: UIColor.darkGrayColor(), fontSize: 14.0)
    private lazy var saveButton = UIButton(title: "保 存", bgColor: UIColor.darkGrayColor(), fontSize: 14.0)
    
    //MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置UI
        setupUI()
        
        //注册cell
        collectionView.registerClass(PhotoCell.self, forCellWithReuseIdentifier: photoCellID)
        
        collectionView.scrollToItemAtIndexPath(pictureIndexPath!, atScrollPosition: .Left, animated: false)
    
    }
    
    override func loadView() {
        super.loadView()
        view.frame.size.width += 15
    }
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

//MARK:-设置UI
extension PhotoBrowserViewController {
    /// 设置界面
    func setupUI() {
        //添加子控件
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        
        //设置尺寸位置
        collectionView.frame = view.bounds
        collectionView.dataSource = self
        collectionView.delegate = self
        let buttonW : CGFloat = 90
        let buttonH : CGFloat = 35
        let closeButtonX : CGFloat = 20
        let closebuttonY : CGFloat = UIScreen.mainScreen().bounds.height - buttonH - 20
        closeButton.frame = CGRect(x: closeButtonX, y: closebuttonY, width: buttonW, height: buttonH)
        let saveButtonX : CGFloat = UIScreen.mainScreen().bounds.width - buttonW - 20
        saveButton.frame = CGRect(x: saveButtonX, y: closebuttonY, width: buttonW, height: buttonH)
        
        //设置button的点击事件
        closeButton.addTarget(self, action: "closeClick", forControlEvents: .TouchUpInside)
        saveButton.addTarget(self, action: "saveClick", forControlEvents: .TouchUpInside)
    }
}

//MARK:-监听按钮的点击事件
extension PhotoBrowserViewController {
    /// 点击关闭按钮
    func closeClick() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    /// 点击保存按钮
    func saveClick() {
        let photoCell = collectionView.visibleCells().first as! PhotoCell
        
        UIImageWriteToSavedPhotosAlbum(photoCell.imageView.image!, nil, nil, nil)
        
        let alerVC = UIAlertController(title: "提示", message: "保存成功", preferredStyle: .Alert)
        let action1 = UIAlertAction(title: "确定", style: .Default, handler: nil)
        alerVC.addAction(action1)
        presentViewController(alerVC, animated: false, completion: nil)
    }
}

//MARK:- collectionView的数据源代理
extension PhotoBrowserViewController : UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureArray?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(photoCellID, forIndexPath: indexPath) as! PhotoCell
        
        cell.picture = pictureArray?[indexPath.item]
        
        return cell
    }
}


extension PhotoBrowserViewController : DismissDelegate {
    func currentIndexPath() -> NSIndexPath {
        let cell = (collectionView.visibleCells().first)!
        let indexPath = collectionView.indexPathForCell(cell)
        
        return indexPath!
    }
    
    func imageView() -> UIImageView {
        let imageView = UIImageView()
        
        let cell = (collectionView.visibleCells().first)! as! PhotoCell
        
        imageView.image = cell.imageView.image
        
        //设置属性
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
        
    }
}

